package com.yuanlrc.base.controller.admin;


import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.admin.Clazz;
import com.yuanlrc.base.service.admin.ClazzService;
import com.yuanlrc.base.service.admin.FacultiesService;
import com.yuanlrc.base.service.admin.OperaterLogService;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 *
 * 班级列表管理控制器
 */
@Controller
@RequestMapping("/clazz")
public class ClazzController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private FacultiesService facultiesService;

    @Autowired
    private OperaterLogService operaterLogService;


    /**
     * 班级列表页面
     * @param model
     * @param clazz
     * @param pageBean
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model, Clazz clazz, PageBean<Clazz> pageBean){
        model.addAttribute("title", "班级列表");
        model.addAttribute("pageBean", clazzService.findList(clazz, pageBean));
        return "admin/clazz/list";
    }

    /**
     * 新增班级列表页面
     * @return
     */
    @RequestMapping(value="/add",method= RequestMethod.GET)
    public String add(Model model){
        model.addAttribute("clazzs",clazzService.finAll());
        model.addAttribute("faculties",facultiesService.findAll());
        return "admin/clazz/add";
    }

    /**
     * 班级添加表单提交处理
     * @param clazz
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Clazz clazz){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(clazz);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        //判断班级编号是否存在
        if(clazzService.isExistByNum(clazz.getNum())){
            return Result.error(CodeMsg.ADMIN_CLAZZNUM_EXIST);
        }
        //判断班级名是否存在
        if(clazzService.isExistByName(clazz.getName(), 0l)){
            return Result.error(CodeMsg.ADMIN_CLAZZNAME_EXIST);
        }
        //到这说明一切符合条件，进行数据库新增
        if(clazzService.save(clazz) == null){
            return Result.error(CodeMsg.ADMIN_CLAZZ_ADD_ERROR);
        }
        operaterLogService.add("添加班级，班级名：" + clazz.getName());
        return Result.success(true);
    }


    /**
     * 班级列表编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id",required=true)Long id){
        model.addAttribute("faculties",facultiesService.findAll());
        model.addAttribute("clazz", clazzService.find(id));
        return "admin/clazz/edit";
    }

    /**
     * 编辑班级信息表单提交处理
     * @param clazz
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Clazz clazz){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(clazz);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(clazz.getFaculties() == null || clazz.getFaculties().getId() == null){
            return Result.error(CodeMsg.ADMIN_CLAZZ_FACULTIES_EMPTY);
        }
        if(clazz.getId() == null || clazz.getId().longValue() <= 0){
            return Result.error(CodeMsg.ADMIN_CLAZZ_NO_EXIST);
        }
        //判断班级名是否存在
        if(clazzService.isExistByName(clazz.getName(), clazz.getId())){
            return Result.error(CodeMsg.ADMIN_CLAZZNAME_EXIST);
        }
        //到这说明一切符合条件，进行数据库保存
        Clazz findById = clazzService.find(clazz.getId());
        //讲提交的用户信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
        BeanUtils.copyProperties(clazz, findById, "id","createTime","updateTime");
        if(clazzService.save(findById) == null){
            return Result.error(CodeMsg.ADMIN_CLAZZ_EDIT_ERROR);
        }
        operaterLogService.add("编辑班级，班级名：" + clazz.getName());
        return Result.success(true);
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
        try {
            clazzService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_CLAZZ_DELETE_ERROR);
        }
        log.info("编辑班级ID【"+id+"】");
        operaterLogService.add("删除班级ID【"+id+"】");
        return Result.success(true);
    }

}
